package com.briup.bookstore.web.controller;

import com.briup.bookstore.response.Result;
import com.briup.bookstore.service.BookService;
import com.briup.bookstore.vo.BookInfoVO;
import com.briup.bookstore.vo.BookVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author briup-adam
 * @Date 2023/7/25 下午2:15
 * @Description
 **/


@RestController
@Api(tags = "图书模块")
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("showBooks")
    @ApiOperation("首页展示图书信息 只展示20本")
    public  Result showBooks(){
        List<BookVO> books = bookService.showBooks();
        return  Result.success(books);
    }
    @GetMapping("searchBook")
    @ApiOperation("图书信息检索根据书名、作者、出版社")
    public Result searchBook(String query){
        List<BookVO> list = bookService.getBook(query);
       return Result.success(list);
    }
    @GetMapping("findByCategory/{id}")
    @ApiOperation("图书信息检索根据分类")
    public  Result findByCategory(@PathVariable Integer id){
        List<BookVO> list = bookService.getBooksByCategoryId(id);
        return Result.success(list);
    }
    @GetMapping("/{id}")
    @ApiOperation("图书详情获取")
    public  Result getBook(@PathVariable Integer id, HttpServletRequest request){
        String userId = (String) request.getAttribute("userId");
        BookInfoVO book = bookService.getBookById(id,Integer.parseInt(userId));
        return  Result.success(book);
    }


}
